﻿Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

Public Class frmTrialBalance

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            If Me.REnglish.Checked = True Then
                ShowEnglish()

            ElseIf Me.RArabic.Checked = True Then
                ShowArabic()
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmTrialBalance_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Sub ShowEnglish()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim dap As New SqlDataAdapter("Select Distinct AccNo,Acc1,Acc4 Acc2," & _
                                          "dbo.GetAccBalanceStart(AccNo,N'" & Me.DTPPeriodFrom.Value.ToShortDateString & " 00:00:00') TotalIn," & _
                                          "dbo.GetAccMovement(AccNo,N'" & Me.DTPPeriodFrom.Value.ToShortDateString & " 00:00:00'," & _
                                                                "N'" & Me.DTPPeriodTo.Value.ToShortDateString & " 23:59:59') Done," & _
                                          "dbo.GetAccBalanceClose(AccNo,N'" & Me.DTPPeriodTo.Value.ToShortDateString & " 23:59:59') TotalOut " & _
                                          "From Transactions", cnn)
            Dim das As New DataSet

            dap.SelectCommand.CommandTimeout = 10000
            dap.Fill(das, "Transactions")

            Dim rpt As New TrialBalance
            rpt.SetDataSource(das)

            'Create Parameters
            Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions
            Dim crParameterFieldLocation As ParameterFieldDefinition
            Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues
            crParameterFieldDefinitions = rpt.DataDefinition.ParameterFields

            'MinDate PARAMETER
            crParameterFieldLocation = crParameterFieldDefinitions.Item("MinDate")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = Me.DTPPeriodFrom.Value
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

            'MaxDate PARAMETER
            crParameterFieldLocation = crParameterFieldDefinitions.Item("MaxDate")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = Me.DTPPeriodTo.Value
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

            Me.CrystalReportViewer1.ReportSource = rpt

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub ShowArabic()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim dap As New SqlDataAdapter("Select Distinct AccNo," & _
                                          "IsNull(dbo.GetAccNameArabic(Acc1),Acc1) Acc1,IsNull(dbo.GetAccNameArabic(Acc4),Acc4) Acc2,Acc4," & _
                                          "dbo.GetAccBalanceStart(AccNo,N'" & Me.DTPPeriodFrom.Value.ToShortDateString & " 00:00:00') TotalIn," & _
                                          "dbo.GetAccMovement(AccNo,N'" & Me.DTPPeriodFrom.Value.ToShortDateString & " 00:00:00'," & _
                                                                   "N'" & Me.DTPPeriodTo.Value.ToShortDateString & " 23:59:59') Done," & _
                                          "dbo.GetAccBalanceClose(AccNo,N'" & Me.DTPPeriodTo.Value.ToShortDateString & " 23:59:59') TotalOut " & _
                                          "From Transactions", cnn)
            Dim das As New DataSet

            dap.SelectCommand.CommandTimeout = 10000
            dap.Fill(das, "Transactions")

            Dim rpt As New TrialBalanceArabic
            rpt.SetDataSource(das)

            'Create Parameters
            Dim crParameterDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions
            Dim crParameterFieldLocation As ParameterFieldDefinition
            Dim crParameterValues As New CrystalDecisions.Shared.ParameterValues
            crParameterFieldDefinitions = rpt.DataDefinition.ParameterFields

            'MinDate PARAMETER
            crParameterFieldLocation = crParameterFieldDefinitions.Item("MinDate")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = Me.DTPPeriodFrom.Value
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

            'MaxDate PARAMETER
            crParameterFieldLocation = crParameterFieldDefinitions.Item("MaxDate")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = Me.DTPPeriodTo.Value
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

            Me.CrystalReportViewer1.ReportSource = rpt

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class